package com.appcenter.application.service;

import com.appcenter.application.dto.RoleUserDto;
import com.appcenter.application.input.RoleUser.*;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.mapper.RoleUsersMapper;
import com.appcenter.application.mapper.RolesMapper;
import com.appcenter.application.mapper.UsersMapper;
import com.appcenter.application.model.RoleUsers;
import com.appcenter.application.model.Roles;
import com.appcenter.application.model.Users;
import com.appcenter.application.output.RoleUser.*;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.UUID;

@Service
public class RoleUserService {
    @Autowired
    RoleUsersMapper roleUsersMapper;
    @Autowired
    UsersMapper usersMapper;

    @Autowired
    AppsMapper appsMapper;
    @Autowired
    RolesMapper rolesMapper;


    public CreateRoleUserOutput CreateRoleUser(CreateRoleUserInput input) {
        CreateRoleUserOutput output = new CreateRoleUserOutput();

        Users user = usersMapper.getUsersByUserCode(input.getItem().getUserCode());
        if (user == null) {
            throw new ErrorJsonResult("1000071", "用户不存在！");
        }
        Example example = new Example(RoleUsers.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("RoleId", input.getItem().getRoleId());
        criteria.andEqualTo("UserId", user.getId());
        List<RoleUsers> countries = roleUsersMapper.selectByExample(example);

        int RoleUserCount = countries.size();
        if (RoleUserCount > 0) {
            throw new ErrorJsonResult("1000071", "该角色下用户已存在！");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        RoleUsers curRoleUser= new RoleUsers();
        curRoleUser.setId(uuid);
        curRoleUser.setRoleId(input.getItem().getRoleId());
        curRoleUser.setUserId(user.getId());
        roleUsersMapper.insertSelective(curRoleUser);
        output.setItem(curRoleUser);
        return output;
    }

    public DeleteRoleUserOutput DeleteRoleUser(DeleteRoleUserInput input) {
        DeleteRoleUserOutput output = new DeleteRoleUserOutput();
        roleUsersMapper.deleteByPrimaryKey(input.getItem().getId());
        return output;

    }

    public GetRoleUsersOutput GetRoleUsers(GetRoleUsersInput input) {
        GetRoleUsersOutput output = new GetRoleUsersOutput();
        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"Id desc");
        List<RoleUserDto> ls = roleUsersMapper.searchRoleUser(input);
        PageInfo<RoleUserDto> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<RoleUserDto>(page.getList(),(int)page.getTotal()));
        return output;
    }

    public PageLoadOutput PageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(GetRoleUsers(input).getItems());
        output.setApps(appsMapper.selectAll());
        output.setRoles(rolesMapper.selectAll());
        return output;
    }
    //---------RoleUserService-------------
    public GetRoleUsersServiceOutput GetRoleUsers(GetRoleUsersServiceInput input) {
        GetRoleUsersServiceOutput output = new GetRoleUsersServiceOutput();
        List<RoleUserDto> ls=roleUsersMapper.getRoleUsers(input);
        output.setItems(new PagedResult<RoleUserDto>(ls,ls.size()));
        return output;
    }

}
